package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.BuyerBean;
import com.project.bean.BuyerCarBean;
import com.project.bean.BuyerInfoBean;
import com.project.bean.UserBean;
import com.project.mapper.IBuyCarMapper;
import com.project.mapper.IBuyerInfoMapper;
import com.project.mapper.IBuyerMapper;
import com.project.mapper.IProductMapper;
import com.project.service.IBuyerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class BuyerServiceImpl implements IBuyerService {
    @Autowired
    private IBuyerMapper buyMapper;
    @Autowired
    private IBuyCarMapper buyCarMapper;;
    @Autowired
    private IBuyerInfoMapper buyerInfoMapper;

    @Override
    public void addOrder(BuyerBean buyer, Integer userId) {
        //完成采购信息添加
       buyMapper.insert(buyer);
        QueryWrapper<BuyerCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",userId);

        List<BuyerCarBean> buyerCarList =buyCarMapper.selectList(qw);

        buyerInfoMapper.addInfo(buyer.getId(),buyerCarList);


        buyCarMapper.delete(qw);
        }




    @Override
    public IPage<BuyerBean> findByItem(Integer pageNO, String supplierName, LocalDate startDate, LocalDate endDate) {

        return buyMapper.findByItem(new Page(pageNO,3), supplierName, startDate, endDate);
    }

    @Override
    public BuyerBean findById(Integer buyId) {
        BuyerBean buyerBean = buyMapper.findById(buyId);
        buyerBean.setBuyerInfoList(buyerInfoMapper.findList(buyId));

        return buyerBean;
    }
}
